package com.blindBox.back.controller.getDataController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blindBox.back.annotation.JwtToken;
import com.blindBox.back.commonResult.MsgBackToFront;
import com.blindBox.back.domain.Topic;
import com.blindBox.back.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

//话题管理
@RestController
@RequestMapping("/topic")
public class BackDataTopicController
{
    @Autowired
    private TopicService topicService;

   //返回所有的话题数据
   @JwtToken
   @PostMapping("/get_all_topics")
    public MsgBackToFront get_all_topics()
   {
       List<Map<Object, Object>> allPapersWithUsers = topicService.getAllTopicsWithUsers();
       return new MsgBackToFront(allPapersWithUsers,MsgBackToFront.HANDLE_SUCCESS);
    }

   //增加一个话题---管理员专用
   @JwtToken
   @PostMapping("/add_one_topic")
   public MsgBackToFront add_one_topic(@RequestParam("topic_name")String topic_name)
   {
       Topic topic = new Topic();
       topic.setContents(topic_name);
       boolean save = topicService.save(topic);
       return save?new MsgBackToFront(null,MsgBackToFront.HANDLE_SUCCESS):
               new MsgBackToFront(null,MsgBackToFront.DATABASE_ERROR);
   }

    //设置当前话题为不活跃--逻辑删除该话题
    @JwtToken
    @PostMapping("/set_topic_dead")
    public MsgBackToFront del_one_topic(@RequestParam("topic_id")Integer id)
    {
        boolean removeById = topicService.removeById(id);
        return removeById?new MsgBackToFront(null,MsgBackToFront.HANDLE_SUCCESS):
                new MsgBackToFront(null,MsgBackToFront.DATABASE_ERROR);
    }

    //设置当前话题为活跃
    @JwtToken
    @PostMapping("/set_topic_alive")
    public MsgBackToFront set_topic_alive(@RequestParam("topic_id")Integer id)
    {
        //恢复逻辑删除的记录为不删除
        boolean removeById = topicService.updateDeletedBoolean(id);
        return removeById?new MsgBackToFront(null,MsgBackToFront.HANDLE_SUCCESS):
                new MsgBackToFront(null,MsgBackToFront.DATABASE_ERROR);
    }


    //删除一个话题----真实删除




    //模糊搜索
    @JwtToken
    @PostMapping("/get_target_topic")
    public MsgBackToFront get_target_topic(@RequestParam("topic_name")String topic_name)
    {
        List<Topic> contents = topicService.list(new QueryWrapper<Topic>().like("contents", topic_name));
        return new MsgBackToFront(contents,MsgBackToFront.HANDLE_SUCCESS);
    }
}
